// pages/poster/poster.js
const app = getApp();
const db = wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    poster_info: '',
    painting: '',
    img_url: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    that.setData({
      poster_info: JSON.parse(options.poster_info)
    });
    console.log('poster_info', this.data.poster_info)

    wx.cloud.getTempFileURL({
        fileList: [that.data.poster_info.img_url],
        success(res) {
          console.log("temp_url", res)
          that.setData({
            img_url: res.fileList[0].tempFileURL
          })
          console.log("img_url", that.data.img_url)
          that.eventDraw();
        }
      })
  },
  // 作图
  eventDraw() {
    let that = this
    let info = that.data.poster_info
    let img_url = this.data.img_url
    console.log('info', info._id)

    wx.showLoading({
      title: '绘制海报中...',
      duration: 2000,
    });

    // 调用与云函数生成二维码,上线才能显示
    // wx.cloud.callFunction({
    //   name: 'qrCode',
    //   data:{
    //     scene: info._id
    //   },
    //   success(res){
    //     console.log(res)
    //   }
    // })

    that.setData({
      painting: {
        width: 375,
        height: 555,
        clear: true,
        views: [
          {
            type: 'text', //标题
            content: info.title + info.detail,
            fontSize: 30,
            lineHeight: 21,
            color: '#383549',
            textAlign: 'left',
            top: 330,
            left: 10,
            width: 355,
            MaxLineNumber: 2,
            breakWord: true,
            bolder: true
          },
          {
            type: 'text', // 价格
            content: '￥' + info.price,
            fontSize: 40,
            color: '#E62004',
            textAlign: 'left',
            top: 375,
            left: 5,
            MaxLineNumber: 1,
            bolder: true
          },
          {
            type: 'text', // 电话
            content: "联系电话: " + info.phone,
            fontSize: 20,
            color: '#0C0910',
            textAlign: 'left',
            top: 420,
            left: 10,
            MaxLineNumber: 1,
            bolder: true
          },
          {
            type: 'image', //商品图片
            url: img_url,
            top: 15,
            left: 10,
            width: 355,
            height: 300
          },
          // {
          //   type: 'image', //页面二维码
          //   url: qrurl,
          //   top: 500,
          //   left: 10,
          //   width: 300,
          //   height: 300
          // },
        ]
      }
    })
  },

  // 保存图片 
  eventSave() {
    wx.saveImageToPhotosAlbum({
      filePath: this.data.shareImage,
      success(res) {
        wx.showToast({
          title: '保存图片成功',
          icon: 'success',
          duration: 2000
        })
      },
      fail() {
        wx.showToast({
          title: '保存失败',
          icon: 'fail',
          duration: 2000
        })
      }
    })
  },

  // 生成图片
  eventGetImage(event) {
    console.log("event", event)
    wx.hideLoading()
    const {
      tempFilePath
    } = event.detail
    this.setData({
      shareImage: tempFilePath
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})